/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.control.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import flash.system.Capabilities;
	import flash.utils.Timer;
	
	import multimedia.control.event.LoadSlideEvent;
	import multimedia.model.ModelLocator;
	import multimedia.model.vo.MediaElementVO;
	
	import org.osmf.elements.SlideElement;
	import org.osmf.traits.LoadTrait;
	import org.osmf.traits.MediaTraitType;

	public class LoadSlideCommand extends SequenceCommand implements ICommand
	{

		private var e:LoadSlideEvent;
		private var model:ModelLocator;

		private var timer:Timer;

		private var currentSlide:int;
		
		private var _loadSlideCalled:Boolean = false;

		override public function execute(event:CairngormEvent):void
		{
			if(_loadSlideCalled)
			{
				return;
			}
			_loadSlideCalled = true;
			this.model = ModelLocator.getInstance();
			this.e = event as LoadSlideEvent;
			this.nextEvent = e.nextEvent;

			var height:int = flash.system.Capabilities.screenResolutionY;
			var width:int = (e.slide.aspect_ratio == "16:9" ? height * (16 / 9) : height * (4 / 3));

			//for resolution not 16/9
			//we need to start with the width
			if (width > flash.system.Capabilities.screenResolutionX)
			{
				width = flash.system.Capabilities.screenResolutionX;
				height = (e.slide.aspect_ratio == "16:9" ? width / (16 / 9) : width / (4 / 3));
			}
			var slide:SlideElement = new SlideElement(e.slide);
			
			(slide.getTrait(MediaTraitType.LOAD) as LoadTrait).load();
			model.mediaElements[(model.counter + 1)%4] = new MediaElementVO(e.slide, slide)
			//model.cursor.insert(new MediaElementVO(e.slide, slide));
			
			executeNextCommand();
		}
	}
}